package com.example.mylibrary.base

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding

abstract class BaseFragment<B : ViewBinding> : Fragment(), BaseView, View.OnClickListener {
    lateinit var viewBinding: B

    fun startNextActivity(fragment: Fragment, bundle: Bundle?, clazz: Class<*>) {
        val intent = Intent(fragment.activity, clazz)
        intent.putExtra("bundle", bundle)
        startActivity(intent)
    }

    fun startNextActivityForResult(
        fragment: Fragment,
        requestCode: Int,
        bundle: Bundle?,
        clazz: Class<*>
    ) {
        val intent = Intent(fragment.activity, clazz)
        intent.putExtra("bundle", bundle)
        startActivityForResult(intent, requestCode)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        viewBinding = getBinding()
        return viewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initView(view)
    }

    abstract fun initView(view: View)

    abstract fun getBinding(): B

    fun viewVisible(vararg views: View) {
        if (views.isNotEmpty()) {
            for (view in views) {
                view.visibility = View.VISIBLE
            }
        }
    }

    fun viewGone(vararg views: View) {
        if (views.isNotEmpty()) {
            for (view in views) {
                view.visibility = View.GONE
            }
        }
    }

    override fun showLoading() {
        TODO("Not yet implemented")
    }

    override fun showSuccess() {
        TODO("Not yet implemented")
    }

    override fun showFailed() {
        TODO("Not yet implemented")
    }

    override fun showNoNet() {
        TODO("Not yet implemented")
    }

    override fun showOnRetry() {
        TODO("Not yet implemented")
    }

    /**
     * 复写了点击事件，子类根据需求实现
     */
    override fun onClick(v: View?) {
        // TODO("复写了点击事件，子类根据需求实现")
    }

}